package com.zhengdongzulin.zuke.common

import android.os.Handler
import android.os.Looper
import android.util.Log
import android.webkit.JavascriptInterface
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import com.zhengdongzulin.zuke.base.BaseActivity
import com.just.agentweb.AgentWeb

/**
 * Created by cenxiaozhong on 2017/5/14.
 * source code  https://github.com/Justson/AgentWeb
 */
class AndroidInterface<T : ViewModel, V : ViewDataBinding>(
    private val agent: AgentWeb,
    private val context: BaseActivity<T, V>
) {
    private val deliver = Handler(Looper.getMainLooper())

    @JavascriptInterface
    fun callAndroid(msg: String) {
        deliver.post {
            Log.i("Info", "main Thread:" + Thread.currentThread())
            Toast.makeText(context.applicationContext, "" + msg, Toast.LENGTH_LONG).show()
        }
        Log.i("Info", "Thread:" + Thread.currentThread())
    }

    @JavascriptInterface
    fun backToApp() {
        context.finish()
    }

    @JavascriptInterface
    fun waitTakeGoods() {
        Toast.makeText(context, "待开发", Toast.LENGTH_SHORT).show()
    }

}